package cn.schoolwow.workflow.module.instance.action.kit;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.workflow.domain.instance.StartWorkFlowRequest;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceQuery;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.module.common.domain.QuickWorkFlowConfig;
import cn.schoolwow.workflow.module.instance.action.service.DeleteWorkFlowInstanceServiceFlow;
import cn.schoolwow.workflow.module.instance.action.service.RevokeWorkFlowInstanceServiceFlow;
import cn.schoolwow.workflow.module.instance.action.service.StartWorkFlowServiceFlow;
import cn.schoolwow.workflow.module.instance.action.service.UpdateWorkFlowInstanceContextDataServiceFlow;
import com.alibaba.fastjson.JSONObject;

public class WorkFlowInstanceActionServiceImpl implements WorkFlowInstanceActionService {
    private QuickWorkFlowConfig quickWorkFlowConfig;

    public WorkFlowInstanceActionServiceImpl(QuickWorkFlowConfig quickWorkFlowConfig) {
        this.quickWorkFlowConfig = quickWorkFlowConfig;
    }

    @Override
    public WorkFlowInstance startWorkFlow(String definitionName){
        StartWorkFlowRequest startWorkFlowRequest = new StartWorkFlowRequest();
        startWorkFlowRequest.definitionName = definitionName;
        return startWorkFlow(startWorkFlowRequest);
    }

    @Override
    public WorkFlowInstance startWorkFlow(StartWorkFlowRequest startWorkFlowRequest) {
        FlowContext flowContext = quickWorkFlowConfig.workflow.startFlow(new StartWorkFlowServiceFlow())
                .putInstanceData(startWorkFlowRequest)
                .execute();
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
        return workFlowInstance;
    }

    @Override
    public void revokeWorkFlowInstance(Long workFlowInstanceId) {
        WorkFlowInstanceQuery workFlowInstanceQuery = new WorkFlowInstanceQuery();
        workFlowInstanceQuery.instanceIdList.add(workFlowInstanceId);
        revokeWorkFlowInstance(workFlowInstanceQuery);
    }

    @Override
    public void revokeWorkFlowInstance(WorkFlowInstanceQuery workFlowInstanceQuery) {
        quickWorkFlowConfig.workflow.startFlow(new RevokeWorkFlowInstanceServiceFlow())
                .putInstanceData(workFlowInstanceQuery)
                .execute();
    }

    @Override
    public void updateWorkFlowInstanceContextData(Long workFlowInstanceId, JSONObject contextData) {
        quickWorkFlowConfig.workflow.startFlow(new UpdateWorkFlowInstanceContextDataServiceFlow())
                .putCurrentCompositeFlowData("workFlowInstanceId", workFlowInstanceId)
                .putCurrentCompositeFlowData("contextData", contextData)
                .execute();
    }

    @Override
    public void deleteWorkFlowInstance(Long workFlowInstanceId) {
        WorkFlowInstanceQuery workFlowInstanceQuery = new WorkFlowInstanceQuery();
        workFlowInstanceQuery.instanceIdList.add(workFlowInstanceId);
        deleteWorkFlowInstance(workFlowInstanceQuery);
    }

    @Override
    public void deleteWorkFlowInstance(WorkFlowInstanceQuery workFlowInstanceQuery) {
        quickWorkFlowConfig.workflow.startFlow(new DeleteWorkFlowInstanceServiceFlow())
                .putInstanceData(workFlowInstanceQuery)
                .execute();
    }
}
